package com.lz.football.module.trainingcamp
{
	import com.common.stdlib.DateUtil;
	import com.common.utils.Reflection;
	import com.common.utils.timeUtils.TimerManager;
	import com.global.GlobalDef;
	import com.liangzhong.events.TrainingcampEvent;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.playerCenter.vo.PlayerVo;
	import com.liangzhong.model.trainingcamp.TrainingcampDataManager;
	import com.liangzhong.model.trainingcamp.vo.TrainingcampVo;
	import com.lz.football.module.playerCenter.ListItem;
	
	import common.BaseView.BaseView;
	import common.BaseView.DisplayMouseEventManager;
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	import flash.text.TextField;
	
	import lz.controls.Alert;
	import lz.controls.HScroll;
	import lz.controls.LButton;

	/**
	 *训练营 
	 * @author Administrator
	 * 
	 */	
	public class TrainingcampView extends BaseView implements ITrainingcamp
	{
		
		/**
		 * 
		 */
		private var _hscroll:HScroll;
		/**
		 *目标容器滚动条 
		 */
		private var _target:Sprite;
		/**
		 * 选中了球员item
		 */
		private var _selectItem:ListItem;
		/**
		 *选中了训练类型item 
		 */
		private var _selectTType:TrainingTypeItem;
		/**
		 *tips 
		 */
		private var tips:TextField;
		/**
		 *开始训练按钮 
		 */
		private var startBtn:SimpleButton;
		/**
		 *训练消耗 
		 */
		private var cost:TextField;
		/**
		 *球员等级 
		 */
		private var Lv:TextField;
		/**
		 *经验条 
		 */
		private var bar:MovieClip;
		/**
		 *经验百分率 
		 */
		private var completeness:TextField;
		/**
		 *升级按钮  
		 */
		private var upLvBtn:LButton;
		/**
		 *训练等级 
		 */
		private var trainingCampLv:TextField;
		/**
		 *升级效果和条件  
		 */
		private var condition:TextField;
		/**
		 * 
		 */
		private var txtTimeDesc:TextField;
		/**
		 * 
		 */
		private var txtLeftTime:TextField;
		/**
		 * 
		 */
		private var _vecItem:Vector.<TrainingTypeItem>;
		/**
		 *剩余时间 秒 
		 */		
		private var leftTime	:int;
		/**
		 *下一等级 
		 */
		private var nextLv:TextField;
		/**
		 *剩余训练次数 
		 */
		private var trainingTime:TextField;
		/**
		 *购买按钮 
		 */
		private var addTrainingBtn:SimpleButton;
		private var arr:Array = ["","进攻、速度训练上限+1","进攻、速度、防守训练上限+1","速度、防守、力量训练上限+1",
			"进攻、力量、控球训练上限+1","防守、控球、抢断训练上限+1","速度、力量、抢断训练上限+1","防守、控球、抢断训练上限+1","力量、控球、抢断训练上限+1","","",""]
		public function TrainingcampView()
		{
			super();
		}
		override public function initUI(view:Object=null):void
		{
			
			symbolSkin = Reflection.createInstance("com.lz.modul.trainingcampView");
			this.addChild(symbolSkin);
			tips = symbolSkin.tips;
			_windowBg = symbolSkin.trainingcampBg;
//			super.initUI();
			_viewWidth = _windowBg.width;
			_viewHeight = this.height;
			isDrag = true;
			isCloseBtn = true;
			moveClose(_viewWidth - 30,30);
			onResizeHandler();
			_vecItem = new Vector.<TrainingTypeItem>();
			var item:TrainingTypeItem;
			for(var i:int = 0;i < 6;i++)
			{
				item = new TrainingTypeItem();
				item.initUI();
				item.x = 117 * i + 23;
				item.y = 206;
				switch(i)
				{
					case 0:
						item.trainingType = TrainingcampDataManager.ATTACK;
						break;
					case 1:
						item.trainingType = TrainingcampDataManager.SPEED;
						break;
					case 2:
						item.trainingType = TrainingcampDataManager.CONTROL;
						break;
					case 3:
						item.trainingType = TrainingcampDataManager.POWER;
						break;
					case 4:
						item.trainingType = TrainingcampDataManager.STEAL;
						break;
					case 5:
						item.trainingType = TrainingcampDataManager.DEFENCE;
						break;
					
				}
				
				
				
				//				item.tips = {type:i}
				item.callBack = selectTTypeItem;
				_vecItem.push(item);
				this.addChild(item);
				
			}
			bar = symbolSkin.bar;
			Lv = symbolSkin.Lv;
			completeness = symbolSkin.completeness;
			trainingTime = symbolSkin.trainingTime;
			addTrainingBtn = symbolSkin.addTrainingBtn;
			startBtn = symbolSkin.startBtn;
			showPlayer();
			initScroll();
			addEvtListener();
			
			uptraining(null);
			return;
			upLvBtn = new LButton(symbolSkin.upLvBtn);
			nextLv = symbolSkin.upLvBtn.nextLv;
			upLvBtn.enable = true;
			Lv = symbolSkin.bar.Lv;
			bar = symbolSkin.bar.bar.bar;
			trainingCampLv = symbolSkin.trainingCampLv;
			condition = symbolSkin.condition;
			txtTimeDesc = symbolSkin.txtTimeDesc;
			txtLeftTime = symbolSkin.txtLeftTime;
			cost = symbolSkin.cost;
			nextLv.text = "";
			completeness = symbolSkin.bar.completeness;
			startBtn = symbolSkin.startBtn;
			bar.width = 560 /2;
			
			
			
//			_vecItem[0].tips = {type:TrainingCampDataConstant.ATTACK}
//			_vecItem[1].tips = {type:TrainingCampDataConstant.SPEED}
//			_vecItem[2].tips = {type:TrainingCampDataConstant.DEFENCE}
//			_vecItem[3].tips = {type:TrainingCampDataConstant.POWER}
//			_vecItem[4].tips = {type:TrainingCampDataConstant.CONTROL}
//			_vecItem[5].tips = {type:TrainingCampDataConstant.STEAL}
			
			
			updata();
			addEvtListener();
			
			
		
			
			showBaseInfo();
		}
		/**
		 *显示基本信息 
		 * 
		 */
		private function showBaseInfo():void
		{
			trainingCampLv.text = "训练营Lv" + AppData.getInstance().trainingCampDataManager.trainingcampInfo.level;
			condition.text = "升级条件："+AppData.getInstance().trainingCampDataManager.trainingcampInfo.coin+
				"欧元，俱乐部等级"+AppData.getInstance().trainingCampDataManager.trainingcampInfo.needlevel+"级\n下一级收益："+
				arr[AppData.getInstance().trainingCampDataManager.trainingcampInfo.level];
			nextLv.text = (AppData.getInstance().trainingCampDataManager.trainingcampInfo.level + 1 )+"";
//			LvMc.gotoAndStop(_playerCenterDate.playerCenterVo.level +1);
			
//			txtFactor.text = "升级需要欧元"+AppData.getInstance().playerCenterDataManager.playerCenterVo.coin ;
//			txtAddNum.text = "增加一个球员格";
			//冷却时间
			var curSeconds 		:Number = AppData.getInstance().time;
			//当前时间与上次升级时间间隔 秒
			var grapTime :Number = curSeconds - AppData.getInstance().trainingCampDataManager.trainingcampInfo.pretime
			//如果大于冷却时间 可以再次升级
			if(grapTime >= AppData.getInstance().trainingCampDataManager.trainingcampInfo.freezingTime)
			{
				txtLeftTime.visible = false;
				txtTimeDesc.visible = false;
				upLvBtn.visible = true;
				TimerManager.instance.removeFunction(onTiming);
				txtLeftTime.removeEventListener(TextEvent.LINK,onDropTimeHandler);
			}
			else//显示冷却时间
			{
				leftTime = AppData.getInstance().trainingCampDataManager.trainingcampInfo.freezingTime - grapTime;
				txtLeftTime.visible = true;
				txtTimeDesc.visible = true;
				upLvBtn.visible = false;
				txtLeftTime.htmlText = "" + DateUtil.formatShowHMS(leftTime,true);
				txtLeftTime.addEventListener(TextEvent.LINK,onDropTimeHandler);
				TimerManager.instance.addFunction(onTiming);
			}
			if(AppData.getInstance().trainingCampDataManager.trainingcampInfo.level >= 9)
			{
				upLvBtn.visible = false;
				TimerManager.instance.removeFunction(onTiming);
				txtLeftTime.visible = false;
				txtTimeDesc.visible = false;
				condition.text = "";
			}
		}
		/**
		 *时间计时 
		 */		
		private function onTiming():void
		{
			leftTime --;
			if(leftTime <= 0)
			{
				txtLeftTime.visible = false;
				txtTimeDesc.visible = false;
				upLvBtn.visible = true;
				TimerManager.instance.removeFunction(onTiming);
			}
			else
			{
				txtLeftTime.htmlText = "<u><a href='event:dropTime'>" + DateUtil.formatShowHMS(leftTime,true) + "</a><u>";
			}
		}
		/**
		 *去除冷却时间 
		 * @param e
		 * 
		 */		
		private function onDropTimeHandler(e :Event):void
		{
			var info :String = "您确定花费200点券去除冷却时间?";
			Alert.show(info,"",Alert.YES | Alert.NO,GlobalDef.AlertContainer,dropTimeAlertHandler);
		}
		/**
		 * 去除冷却时间 确认
		 * @param type
		 * 
		 */		
		private function dropTimeAlertHandler(type :int):void
		{
			if(type == Alert.YES)
			{
				this.dispatchEvent(new TrainingcampEvent(TrainingcampEvent.CLEAR_UP_TIME));
			}
		}
		/**
		 * 
		 * 
		 */
		override public function addEvtListener():void
		{
			super.addEvtListener();
			startBtn.addEventListener(MouseEvent.CLICK,onStartHandler);
			addTrainingBtn.addEventListener(MouseEvent.CLICK,onaddTrainingTimeHandler);
//			upLvBtn.addEventListener(MouseEvent.CLICK,onUpLvHandler);
		}
		
		protected function onaddTrainingTimeHandler(event:MouseEvent):void
		{
			// TODO Auto-generated method stub
			if(AppData.getInstance().clubDatamanager.clubInfo.cashpoint <10)
			{
//				Alert.showErrorTip("点券不足，不可以购买！");
				Alert.noCashAlert("消耗10点卷可增加1点训练次数。现有点券不足，是否前往充值");
				return;
			}
			Alert.show("你确定消耗10点券增加1次训练次数吗?","",Alert.NO,GlobalDef.AlertContainer,addTraining);
			
		}
		private function addTraining(type:int):void
		{
			if(type == Alert.YES)
			{
				this.dispatchEvent(new TrainingcampEvent(TrainingcampEvent.BUY_TRAININGTIME));	
			}
		}
		override public function removeEvtListener():void
		{
			super.removeEvtListener();
			startBtn.removeEventListener(MouseEvent.CLICK,onStartHandler);
			addTrainingBtn.removeEventListener(MouseEvent.CLICK,onaddTrainingTimeHandler);
			DisplayMouseEventManager.instance.removeTipsDisplay(bar);
//			upLvBtn.removeEventListener(MouseEvent.CLICK,onUpLvHandler);
//			txtLeftTime.removeEventListener(TextEvent.LINK,onDropTimeHandler);
		}
		override public function destroyView():void
		{
			removeEvtListener();
			while(_vecItem.length)
			{
				_vecItem.pop().destroyView();
			}
			
			var item:ListItem
			while(_target.numChildren)
			{
				item = _target.removeChildAt(0) as ListItem;
				item.destroyView();
			}
			TimerManager.instance.removeFunction(onTiming);
			_hscroll.destroy();
			_target = null;
			_vecItem = null;
			super.destroyView();
//			if(this.parent)
//				this.parent.removeChild(this);
			symbolSkin = null;
			
		}
		/**
		 *训练营升级 
		 * @param event
		 * 
		 */
		protected function onUpLvHandler(event:Event):void
		{
			if(AppData.getInstance().trainingCampDataManager.trainingcampInfo.coin > AppData.getInstance().clubDatamanager.clubInfo.coin)
			{
				Alert.showErrorTip("欧元不足，不可以升级");
				return ;
			}
			if(AppData.getInstance().trainingCampDataManager.trainingcampInfo.needlevel > AppData.getInstance().clubDatamanager.clubInfo.level)
			{
				Alert.showErrorTip("欧元不够，不可以升级");
				return ;
			}
			Alert.show("是否花"+AppData.getInstance().trainingCampDataManager.trainingcampInfo.coin+"欧元升级训练营","",Alert.NO,GlobalDef.AlertContainer,onUpLv);
		}
		private function onUpLv($type:int):void
		{
			if($type == Alert.YES)
			{
				this.dispatchEvent(new TrainingcampEvent(TrainingcampEvent.UP_TRAININGCAMP));
			}
		}
		
		protected function onStartHandler(event:MouseEvent):void
		{
			// TODO Auto-generated method stub
			if(_selectItem == null)
			{
				Alert.showErrorTip("请选择训练的球员");
				return;
			}
			if(_selectTType == null)
			{
				Alert.showErrorTip("请选择训练的类型");
				return;
			}
			if(_selectTType.supTime > 0)
			{
				Alert.showErrorTip("冷却时间还未到，不可以训练此训练位");
				return;
			}
			//trace(AppData.getInstance().clubDatamanager.clubInfo.coin );
//			if(AppData.getInstance().clubDatamanager.clubInfo.coin < _selectTType.cost)
//			{
//				Alert.showErrorTip("欧元不足，不可以购买");
//				return;
//			}
//			if(_selectTType.useTime <= 0)
//			{
//				Alert.show("是否花"+/*_selectTType.buyTime **/ 500000+"欧元增加" +TrainingCampDataConstant.trining(_selectTType.type)+ "训练位","",Alert.NO,GlobalDef.AlertContainer,buyTrining);
//				return;
//			}
			// 判断是否大于25级
			var playerVo:PlayerVo = _selectItem.data as PlayerVo;
			if(playerVo.level >= 25)
			{
				Alert.showErrorTip("球员已经达到25级，不可以训练");
				return;
			}
			
			if(AppData.getInstance().trainingCampDataManager.trainingcampInfo.traincount <= 0)
			{
				Alert.showErrorTip("训练次数不足，不可以训练！");
				return;
			}
			if(AppData.getInstance().clubDatamanager.clubInfo.coin < _selectTType.cost)
			{
				Alert.showErrorTip("欧元不足，不可以训练");
				return;
			}
//			Alert.show("你确定消耗"+_selectTType.cost+"欧元进行训练吗？","",Alert.NO,GlobalDef.AlertContainer,trainingPlayer);
			this.dispatchEvent(new TrainingcampEvent(TrainingcampEvent.START_TRAINING,{playerid:(_selectItem.data as PlayerVo).playerid,type:_selectTType.type}));
			//判断训练的类型是否还可以训练
			
			
		}
		private function trainingPlayer($type:int):void
		{
			if($type == Alert.YES)
				this.dispatchEvent(new TrainingcampEvent(TrainingcampEvent.START_TRAINING,{playerid:(_selectItem.data as PlayerVo).playerid,type:_selectTType.type}));
		}
		/**
		 *初始化流动条 
		 * 
		 */
		private function initScroll():void
		{
			if(_hscroll != null)return;
			if(_target == null)_target = new Sprite();
			_hscroll = new HScroll();
			_hscroll.setBtn(symbolSkin._leftBtn,symbolSkin._rightBtn);
			_hscroll.defaultContain = symbolSkin._default;
			_hscroll.setWidth(660);
			_hscroll.defaultTarget = _target;
			_hscroll.speed = 55;
			_hscroll.delayedTime = 0.3;
			_hscroll.current = 0;
			_hscroll.upData();
		}
		/**
		 *显示信息 
		 * 
		 */
		private function showPlayer():void
		{
			if(_target == null)_target = new Sprite();
			var arr:Array = AppData.getInstance().playerCenterDataManager.playerList;
			arr.sortOn(["color","level"],[Array.DESCENDING,Array.DESCENDING]);
			var len:int = arr.length;
			var item:ListItem;
			for(var i:int = 0;i < len;i++)
			{
				item = new ListItem();
				item.initUI();
				item.data = arr[i];
				item.addEvtListener();
				item.isShowTips = true;
				item.callBack = selectPlayerItem;
				item.y = 0;
				item.x = 55 * _target.numChildren;
				_target.addChild(item);
				if(i == 0)
					selectPlayerItem(item);
			}
			if(_hscroll != null)
			{
				_hscroll.current = 0;
				_hscroll.upData();
			}
		}
		/**
		 *选中了球员 
		 * @param $item
		 * 
		 */
		private function selectPlayerItem($item:ListItem):void
		{
			if(_selectItem != null)_selectItem.select = false;
			_selectItem = $item;
			_selectItem.select = true;
		
			var playerVo:PlayerVo = _selectItem.data as PlayerVo;
			var cost:int;
			switch(playerVo.color)
			{
				case 1:
					cost = 100 * playerVo.level;
					break;
				case 2:
					cost = 1000 * playerVo.level;
					break;
				case 3:
					cost = 2000 * playerVo.level;
					break;
				case 4:
					cost = 3000 * playerVo.level;
					break;
				case 5:
					cost = 5000 * playerVo.level;
					break;
				case 6:
					cost = 10000 * playerVo.level;
					break;
				case 7:
					cost = 100000 * playerVo.level;
					break;
				case 8:
					cost = 500000 * playerVo.level;
					break;
				
			}
			if(playerVo.level >= 25)
			{
				cost = 0;
			}
			for each(var item:TrainingTypeItem in _vecItem)
			{
				item.cost = cost;
			}
			var per:int = Math.floor((playerVo.exp / playerVo.nextexp) * 100)
			bar.gotoAndStop(per);
			Lv.text = "Lv."+playerVo.level + "";
			completeness.text = per + "%";
			DisplayMouseEventManager.instance.addTipsDisplay(bar,playerVo.exp + "/" + playerVo.nextexp );
//			Lv.text ="Lv:"+ playerVo.level +  "";
//			var per:int = playerVo.nextexp <=0 ? 1:int(playerVo.exp / playerVo.nextexp);
//			
//			bar.width = 560 * (per);
//			completeness.text = (per * 100) + "%";
//			
//			
//			cost.text = "消耗欧元："+playerVo.level * playerVo.level * 60000 + "";
			
		}
		/**
		 * 选择了训练类型 
		 * @param $item
		 * 
		 */
		private function selectTTypeItem($item:TrainingTypeItem):void
		{
			if(_selectTType != null)_selectTType.select = false;
			_selectTType = $item;
			_selectTType.select = true;
//			tips.text = _selectTType.tips as String;
			
//			trace(_selectTType.type);
//			return;
//			if(AppData.getInstance().clubDatamanager.clubInfo.coin < 500000)
//			{
//				Alert.showErrorTip("欧元不足，不可以购买");
//				return;
//			}
//			if(_selectTType.useTime <= 0)
//			{
//				Alert.show("是否花"+/*_selectTType.buyTime **/ 500000+"欧元增加" +TrainingCampDataConstant.trining(_selectTType.type)+ "训练位","",Alert.NO,GlobalDef.AlertContainer,buyTrining);
//				return;
//			}
			
		}
		private function buyTrining(type:int):void
		{
			if(type == Alert.YES)
				this.dispatchEvent(new TrainingcampEvent(TrainingcampEvent.BUY_TRAININGTIME,{type:_selectTType.type}));
		}
			
		/**
		 *关闭面板 
		 * @param event
		 * 
		 */
		override public function onCloseHandler(event:MouseEvent):void
		{
			this.dispatchEvent(new TrainingcampEvent(TrainingcampEvent.REMOVE_MODEL));
		}
		/**
		 *训练返回 
		 * @param $obj
		 * 
		 */
		public function training($obj:Object):void
		{
			updata();
			trainingTime.text = "" + AppData.getInstance().trainingCampDataManager.trainingcampInfo.traincount;
			if(_selectItem == null)return;
			var playerVo:PlayerVo = _selectItem.data as PlayerVo;
//			cost.text = "消耗欧元："+playerVo.level * playerVo.level * 60000 + "";
//			Lv.text ="Lv:"+ playerVo.level +  "";
		}
		/**
		 *升级返回 
		 * @param $obj
		 * 
		 */
		public function uptraining($obj:Object):void
		{
			updata();
//			showBaseInfo();
			
			trainingTime.text = "" + AppData.getInstance().trainingCampDataManager.trainingcampInfo.traincount;
		}
		/**
		 *清除冷却时间 
		 * @param $obj
		 * 
		 */
		public function clearTime($obj:Object):void
		{
			showBaseInfo();
		}
		/**
		 *更新数据  
		 * 
		 */
		public function updata():void
		{
			var trainingVo:TrainingcampVo = AppData.getInstance().trainingCampDataManager.trainingcampInfo;
			_vecItem[0].updataStatus(trainingVo.atttime);
			_vecItem[1].updataStatus(trainingVo.spetime);
			_vecItem[2].updataStatus(trainingVo.contime);
			_vecItem[3].updataStatus(trainingVo.powtime);
			_vecItem[4].updataStatus(trainingVo.stetime);
			_vecItem[5].updataStatus(trainingVo.deftime);
		}
		/**
		 * 球员升级 
		 * 
		 */
		public function playerUP():void
		{
			if(_selectItem == null)return;
			selectPlayerItem(_selectItem);
		}
	}
}